package com.fenghongzhang.day006;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class MyAdapter extends PagerAdapter {


    private Context context;
    private List<VP> list;

    public MyAdapter(Context context, List<VP> list) {
        this.context = context;
        this.list = list;
    }

    //多少页.
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
    //创建
    /**
     *
     * @param container  vp  所有创建的页面添加到这个容器里面
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
//
//        TextView textView = new TextView(context);
//        textView.setText(list.get(position));
//        container.addView(textView);

        View inflate = LayoutInflater.from(context).inflate(R.layout.vp_layout, null);

        TextView textView = inflate.findViewById(R.id.vp_tv);
        textView.setText(list.get(position).getTitle());

        ImageView imageView = inflate.findViewById(R.id.vp_img);
        //
        imageView.setImageBitmap(BitmapFactory.decodeFile(list.get(position).getPath()));

        container.addView(inflate);

        return inflate;
    }

    //移除一个view
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //这个一定要注释掉
//        super.destroyItem(container, position, object);
        container.removeView((View)object);
    }
}
